Fix phpstan/phpstan#4296: BleedingEdge: Offset '1234' on array<string, Test>&nonEmpty in isset() does not exist.#5148
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ng offset on string-keyed array - In ArrayType::hasOffsetValueType() and getOffsetValueType(), numeric string offsets like '1234' are converted to integers by toArrayKey(), then rejected because IntegerType is not a supertype of StringType key - Added check against original (pre-coercion) offset type so numeric strings are recognized as valid string keys - New regression test in tests/PHPStan/Rules/Variables/data/bug-4296.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When using
isset($map[$value])on anarray<string, Test>where$valueis a numeric string like'1234', PHPStan incorrectly reports "Offset '1234' does not exist." This is a false positive because the array has string keys and'1234'is a valid string.Changes
ArrayType::hasOffsetValueType()insrc/Type/ArrayType.phpto also check the original (pre-coercion) offset type against the key typeArrayType::getOffsetValueType()for consistencytests/PHPStan/Rules/Variables/data/bug-4296.phptestBug4296()intests/PHPStan/Rules/Variables/IssetRuleTest.phpRoot cause
ConstantStringType('1234')->toArrayKey()converts numeric strings toConstantIntegerType(1234)(matching PHP's array key coercion behavior). ThenArrayType::hasOffsetValueType()checks if the key type (StringType) is a supertype of the coerced offset type (ConstantIntegerType), which returnsno. The existing guard condition only checks if the coerced type is a non-constant string — it doesn't account for the case where the original type was a string that got coerced to an integer. The fix adds an additional check against the original offset type before returningno.Test
Added a regression test that reproduces the exact scenario from the issue: building an
array<string, Test>in a loop using string keys fromgetId(), then checking withisset($map[$value])where$valueis the numeric string'1234'. The test expects no errors.Fixes phpstan/phpstan#4296